# task 1
# 登录接口
# 输入用户名称和密码
# 认证成功后显示欢迎信息
# 输错三次密码锁定账号

import getpass

user_file = 'users.txt'
lock_file = 'locked.txt'

with open(user_file, 'r') as f:
    user_profile = f.read().split()

user_dict = {}
for user in user_profile:
    user_dict[user.split(':')[0].strip()] = user.split(':')[1].strip()

with open(lock_file, 'r') as f:
    user_lock = f.read().split()

print('== Login ==')
while True:
    username = input('username: ')
    if username.strip():
        break

if username in user_lock:
    print('Warning: your account has been locked.')
    exit()

if username in user_dict:
    password = getpass.getpass('password: ')
    if password == user_dict[username]:
        print('Welcome, login success.')
    else:
        count = 2
        while count > 0:
            print('You have %d time(s) to input your password' % count)
            password = getpass.getpass('password: ')
            if password == user_dict[username]:
                print('Welcome, login success.')
                break
            count -= 1
        else:
            print('Warning: you failed many times, your accout was locked.')
            with open(lock_file, 'a') as f:
                f.write(username + '\n')
else:
    print('Warning: the user <%s> not registered.' % username)
